package com.moody.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.domain.CustInfo;
import com.moody.hibernate.domain.UserInfo;
import com.moody.service.CustomerService;

@Controller
@RequestMapping("/")
public class CommonController {
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	
	@Resource
	private CustomerService customerService;
	
	@RequestMapping("checkRequest")
	public void checkCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		response.setContentType("text/html; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 response.getWriter().print("{success:true}");
	}
	
	@RequestMapping("/findCustomer")
	public void findCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String conditionKey = request.getParameter("condition");
		String conditionValue = request.getParameter("keyword");
		
		List<CustInfo> custList = new ArrayList<CustInfo>(); 
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		Map<String,String> condition = new LinkedHashMap<String,String>();
		condition.put(conditionKey, conditionValue);
		custList = customerService.findCustByCondition(condition, userId, orgId);
		
		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", custList);
		JSONObject resultJO=JSONObject.fromObject(resultMap);
		
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(resultJO);
	}
	
	@RequestMapping("cityTree")
		public void cityTree(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String cityTree;
		cityTree = "[{leaf:false,text:\"北京\",children:" +
				"[  {text:\"海淀区\",leaf:false, children:" +
				"[{text:\"苏州街\", leaf:true}]},  " +
				"{text:\"朝阳区\",leaf:true}]}]";
		//JSONObject resultJO=JSONObject.fromObject(cityTree);
		
		response.setContentType("text/html; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(cityTree);
	}
	
}
